package com.sskj.boom.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.boom.R;
import com.sskj.boom.R2;
import com.sskj.boom.bean.VcerifyBean;
import com.sskj.boom.presenter.CheckoutPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.PageBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.BOOM_CHECKOUT)
public class CheckoutActivity extends BaseActivity<CheckoutPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.et_boom_hash)
    EditText etBoomHash;
    @BindView(R2.id.et_hash_count)
    EditText etHashCount;
    @BindView(R2.id.btn_check)
    Button btnCheck;
    @Autowired
    String boomHash;
    private SlimAdapter slimAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.boom_activity_checkout;
    }

    @Override
    public CheckoutPresenter getPresenter() {
        return new CheckoutPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        if (!TextUtils.isEmpty(boomHash)) {
            etBoomHash.setText(boomHash);
        }
        tvTitle.setText(App.INSTANCE.getString(R.string.boom_checkoutActivity1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.boom_item_checkout, new SlimInjector<VcerifyBean>() {
            @Override
            public void onInject(VcerifyBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.bg_checkout, ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_solidf7f_cor10));
                } else {
                    injector.backgroundColor(R.id.bg_checkout, ContextCompat.getColor(App.INSTANCE, R.color.transparent));
                }
                injector.text(R.id.tv_boomHash, data.getBoomHash())
                        .text(R.id.tv_boomValue, data.getBoomValue());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    @Override
    public void initData() {
        ClickUtil.click(btnCheck, () -> {
            if (isEmpty(etBoomHash)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_checkoutActivity2));
                return;
            }

            if (isEmpty(etHashCount)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_checkoutActivity3));
                return;
            }
            mPresenter.toVcerify(getText(etBoomHash), getText(etHashCount));
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CheckoutActivity.class);
        context.startActivity(intent);
    }

    public void toVcerifyOK(List<VcerifyBean> data) {
        if (data == null) {
            return;
        }
        slimAdapter.updateData(data);

    }
}
